﻿@using VinhSon.Intranet.Models
@{
    ViewBag.Title = "User Manager";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section script
{
    <script type="text/javascript">
        function handlerCommand(command, record) {
            if (command == 'Edit') {
                window.App.winEdit.loader.url = '@Url.Action("Edit",
                                                             new
                                                             {
                                                                     id = "ID"
                                                             })'.replace('ID', record.data.Id);
                window.App.winEdit.show();
                window.App.winEdit.load();
            } else if (command == 'Delete')
                window.Ext.Msg.show({
                    title: "Confirm",
                    buttons: window.Ext.Msg.YESNO,
                    fn: function(btn) {
                        if (btn == 'yes')
                            request(
                                '@Url.Action("Delete")',
                                { id: record.data.Id },
                                function(result) {
                                    showNotification(window, result, function() {
                                        window.App.gvData.getStore().reload();
                                    });
                                },
                                function() {
                                    window.Ext.Msg.show(
                                        {
                                            title: "Confirm",
                                            buttons: window.Ext.Msg.YES,
                                            icon: window.Ext.Msg.ERROR,
                                            msg: "There some errors occour when update. Please try again later."
                                        });
                                });
                    },
                    icon: window.Ext.Msg.QUESTION,
                    msg: "Do you really want to delete this record?"
                });
        }

        function add() {
            window.App.winAdd.show();
            window.App.winAdd.load();
        }
    </script>
}
@(Html.X()
      .Viewport()
      .Layout(LayoutType.Border)
      .Items(view =>
             {
                 Toolbar topMenu = new Toolbar
                                   {
                                           Region = Region.North
                                   };
                 view.Add(topMenu);
                 topMenu.Add(new ToolbarFill());
                 topMenu.Add(new Button("Add User")
                             {
                                     Icon = Icon.Add,
                                     OnClientClick = "add"
                             });
                 GridPanel grid = new GridPanel
                                  {
                                          Region = Region.Center,
                                          ForceFit = true,
                                          ID = "gvData"
                                  };
                 grid.View.Add(Html.X()
                                   .GridView()
                                   .EnableTextSelection(true));
                 view.Add(grid);

                 grid.ColumnModel.Add(new Column
                                      {
                                              Text = "Name",
                                              DataIndex = "Name"
                                      });
                 grid.ColumnModel.Add(new Column
                                      {
                                              Text = "Username",
                                              DataIndex = "Username"
                                      });
                 grid.ColumnModel.Add(new DateColumn
                                      {
                                              Text = "Created Date",
                                              DataIndex = "CreatedDate",
                                              Format = Constant.DATE_TIME_FORMAT
                                      });
                 grid.ColumnModel.Add(new DateColumn
                                      {
                                              Text = "Last Login Date",
                                              DataIndex = "LastLoginDate",
                                              Format = Constant.DATE_TIME_FORMAT
                                      });
                 grid.ColumnModel.Add(new CheckColumn
                                      {
                                              Text = "Is Activated",
                                              DataIndex = "IsActivated"
                                      });
                 grid.ColumnModel.Add(new CheckColumn
                                      {
                                              Text = "Is Locked-Out",
                                              DataIndex = "IsLockedOut"
                                      });
                 grid.ColumnModel.Add(new Column
                                      {
                                              Text = "Remark",
                                              DataIndex = "Remark"
                                      });
                 grid.ColumnModel.Add(Html.X()
                                          .Column()
                                          .Width(40)
                                          .PaddingSpec("0 10")
                                          .Align(Alignment.Center)
                                          .Commands(c =>
                                                    {
                                                        c.Add(new ImageCommand
                                                              {
                                                                      Icon = Icon.TableEdit,
                                                                      CommandName = "Edit",
                                                              });
                                                        c.Add(new ImageCommand
                                                              {
                                                                      Icon = Icon.Delete,
                                                                      CommandName = "Delete",
                                                              });
                                                    })
                                          .Listeners(c =>
                                                     {
                                                         c.Command.Handler = "handlerCommand(command, record);";
                                                     }));
                 grid.Store.Add(Html.X()
                                    .Store()
                                    .RemoteSort(true)
                                    .RemotePaging(true)
                                    .RemoteGroup(true)
                                    .PageSize(50)
                                    .AutoLoad(true)
                                    .AutoSync(false)
                                    .Model(c => c.Add(Html.X()
                                                          .Model()
                                                          .Fields("Username",
                                                                  "Name",
                                                                  "CreatedDate",
                                                                  "LastLoginDate",
                                                                  "Remark",
                                                                  "IsActivated",
                                                                  "IsLockedOut",
                                                                  "Id")
                                                          .IDProperty("Id")))
                                    .Proxy(c => c.Add(Html.X()
                                                          .AjaxProxy()
                                                          .Json(true)
                                                          .Url(Url.Action("GetList"))
                                                          .ActionMethods(v => v.Read = HttpMethod.POST)
                                                          .Reader(Html.X()
                                                                      .JsonReader()
                                                                      .Root("data")
                                                                      .TotalProperty("total"))))
                                    .AutoLoadParams(c =>
                                                    {
                                                        c.Add(new Parameter("start",
                                                                            "0",
                                                                            ParameterMode.Raw));
                                                        c.Add(new Parameter("limit",
                                                                            "50",
                                                                            ParameterMode.Raw));
                                                    }));
                 grid.BottomBar.Add(new PagingToolbar());
                 grid.Features.Add(Html.X()
                                       .Grouping()
                                       .GroupHeaderTplString("Managed By: {name} ({[values.rows.length]} {[values.rows.length > 1 ? \"items\" : \"item\"]})"));
             }))
@(Html.X()
      .Window()
      .ID("winAdd")
      .Resizable(false)
      .Modal(true)
      .Header(true)
      .Width(380)
      .Height(350)
      .Hidden(true)
      .Loader(new ComponentLoader
              {
                      Url = Url.Action("Add",
                                       new
                                       {
                                               id = ViewBag.Id
                                       }),
                      AutoLoad = false,
                      Mode = LoadMode.Frame
              }))
@(Html.X()
      .Window()
      .ID("winEdit")
      .Resizable(false)
      .Modal(true)
      .Header(true)
      .Width(380)
      .Height(350)
      .Hidden(true)
      .Loader(Html.X()
                  .ComponentLoader()
                  .Url(Url.Action("Edit"))
                  .Mode(LoadMode.Frame)
                  .AutoLoad(false)))